package com.coolxi.controller;

import com.coolxi.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Eoly on 2017/6/2.
 */
@Controller
@RequestMapping("/")
public class TestController {
    @RequestMapping("test")
    public String test(Model model) {
        model.addAttribute("stu", "hello,world!");
        return "test";
    }

    @RequestMapping("login")
    public String login(
            @RequestParam String username,
            @RequestParam String password,
            HttpServletRequest request
    ) {
        if (!username.equals("xiaokeai") || !password.equals("123456")) {
            throw new RuntimeException("用户名或密码错误");
        }
        request.getSession().setAttribute("loginUser", new User(username, password));
        return "redirect:/flower/home.html";
    }

    @RequestMapping("/flower/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("loginUser");
        return "redirect:/user/login.html";
    }
}
